<?php
namespace app\admin\controller;
use think\Db;
use app\common\model\Category;
use  app\common\validate\Article as Art;
class Article extends Base
{
    public function articleList()
    {
        $articleList = Db::name("article")->order('article_id desc')->paginate(5,false,['query'=>request()->param()])->each(function($item, $key){
            $userId = $item["uid"];
            $tagId = $item["category_id"];
            $nickname = Db::name('user')->where("uid",$userId)->value("username");
            $tagName = Db::name('category')->where("id",$tagId)->value("category_name");
            $item['nickname'] = $nickname;
            $item['tagName'] = $tagName;
            return $item;
        });
        $page = $articleList->render();
        return view("/article-list",["articleList" => $articleList]);
    }
    public function delete()
    {
        $articleId = input("articleId");
        $delete = Db::name("article")->where("article_id",$articleId)->delete();
        if($delete){
            return json(["code" => 1,"msg" =>"删除成功"]);
        }else{
            return json(["code" => 0,"msg" =>"删除失败"]);
        }
    }
    public function category()
    {
        $category = Db::name("category")->order("id")->select();
        return view("/category",["category" => $category]);
    }
    public function saveCategory()
    {
        $categoryName = input("categoryName");
        $id = input("id");
        $saveCategory = Db::name("category")->where("id",$id)->update(["category_name" => $categoryName]);
        if ($saveCategory){
            return json(["code" => 1,"msg" =>"保存成功"]);
        }
        return json(["code" => 0,"msg" =>"保存失败"]);
    }
    public function addCategory()
    {
        $categoryName = input("categoryName");
        $insert = Category::create(["category_name" =>$categoryName]);
        if ($insert){
            return json(["code" => 1 ,"msg" => "添加成功"]);
        }
        return json(["code" => 0 ,"msg" => "添加失败"]);
    }
    public function deleteCategory()
    {
        $id = input("id");
        $deleteCategory = Db::name("category")->where("id",$id)->delete();
        if ($deleteCategory){
            return json(["code" => 1,"msg" =>"删除成功"]);
        }
        return json(["code" => 0,"msg" =>"删除失败"]);
    }
    //图片上传
    public function uploads()
    {
        $type = input("type");
        $file = request()->file('Image');
        if($file){
          if($type == "defaultCover"){
            $path =  ROOT_PATH . 'public' . DS . 'uploads' .DS .'default';
            $info = $file->validate(['size'=>5*1024*1024,'ext'=>'jpg,png,gif'])->move($path,"default_cover.jpg");
          }else{
            $path = ROOT_PATH . 'public' . DS . 'uploads';
            $info = $file->validate(['size'=>5*1024*1024,'ext'=>'jpg,png,gif'])->move($path);
          }
            if($info){
                return json([ "code" =>1 ,"msg" => "图片上传成功","url"=> DS . 'uploads' .DS .$info->getSaveName()]);
            }else{
                return json([ "code" =>0 ,"msg" => $file->getError()]);
            }
        }
    }
    //发布文章/修改文章
    public function articleAdd()
    {
        $id = input("id");
        $title = input("title","","strip_tags");
        $tag = input("tag");
        $content = input("content");
        if(request()->isAjax()){
            preg_match('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',input("content","","trim"), $ereg);//正则表达式把图片的SRC获取出来了
            if(empty($ereg)) {
                $img = "";
            }else{
                $img = $ereg[1];//图片SRC
            }
            $data["title"] = $title;
            $data["uid"] = session("uid");
            $data["content"] = $content;
            $data["create_time"] = time();
            $data["category_id"] = $tag;
            $data["img"] = $img;
            $ArticleValidate = new Art();
            if (!$ArticleValidate->check($data)){
                return json(["code" => 0 ,"msg" =>$ArticleValidate->getError()]);
            }
            if($id > 0){
                $update = Db::name("article")->where("article_id",$id)->update($data);
                if($update){
                    return json(["code" => 1,"msg"=>"修改成功"]);
                }
                return json(["code" => 0,"msg"=>"修改失败"]);
            }else{
                $insert = Db::name("article")->insert($data);
                if($insert){
                    return json(["code" => 1,"id" =>$id,"msg"=>"发布成功"]);
                }
                return json(["code" => 0,"msg"=>"发布失败"]);
            }
        }else{
            if($id > 0){
                $articleDetail = Db::name("article")->where("article_id",$id)->find();
                $articleDetail["content"] = str_replace("\r\n", '',$articleDetail["content"]);
            }else{
                $articleDetail = "";
            }
            $tagList = Db::name("category")->select();
            return view("/article-add",["tagList" => $tagList,"articleDetail" => $articleDetail]);
        }
    }
}
